package com.littcore.saap.system.service.impl;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.littcore.common.BeanManager;
import com.littcore.dao.IResultsetTransformer;
import com.littcore.dao.page.IPageList;
import com.littcore.dao.ql.PageParam;
import com.littcore.saap.assistant.vo.UserOpLogVo;
import com.littcore.saap.common.vo.IUserInfo;
import com.littcore.saap.core.web.util.LoginUtils;
import com.littcore.saap.system.dao.UserOpLogDao;
import com.littcore.saap.system.po.UserOpLog;
import com.littcore.saap.system.service.IUserInfoService;
import com.littcore.saap.system.service.IUserOpLogService;
import com.littcore.saap.system.webservice.IUserOpLogWebService;
import com.littcore.util.BeanCopier;

/**
 * .
 * 
 * <pre>
 * <b>描述：</b>
 * 
 * </pre>
 * 
 * <pre>
 * <b>修改记录：</b>
 * 
 * </pre>
 * 
 * @author <a href="mailto:littcai@hotmail.com">蔡源</a>
 * @since 2015年5月11日
 * @version 1.0
 */
public class UserOpLogServiceImpl implements IUserOpLogService, IUserOpLogWebService {
	@Resource
	private UserOpLogDao userOpLogDao;
	@Resource
	private IUserInfoService userService;

	public IPageList listPage(PageParam pageParam) {
		String listHql = "select obj from UserOpLog obj" +
				"-- and obj.tenantId={tenantId}" + 
				"-- and obj.moduleCode={moduleCode}" + 
			    "-- and obj.recordId={recordId}";
		if(!StringUtils.isEmpty(pageParam.getCond("recordIds"))){
			listHql+="or obj.recordId in ("+pageParam.getCond("recordIds")+")";
		}

		return userOpLogDao.listPage(listHql, pageParam);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.littcore.saap.assistant.service.impl.IUserOpLogWebService#findPage(com
	 * .litt.core.dao.ql.PageParam)
	 */
	@Override
	public IPageList findPage(PageParam pageParam) {
		IPageList pageList = this.listPage(pageParam);
		pageList.setResultsetTransformer(new IResultsetTransformer() {

			@Override
			public List transform(List srcList) {
				List<UserOpLogVo> voList = BeanCopier.copyList(srcList, UserOpLogVo.class);
				for (UserOpLogVo userOpLogVo : voList) {
					IUserInfo userInfo = userService.find(userOpLogVo.getOpBy());
					userOpLogVo.setOpUserAvatar(userInfo.getHeadImgUrl());
					userOpLogVo.setAction(BeanManager.getMessage(userOpLogVo.getModuleCode() + ".func." + userOpLogVo.getFuncCode(), LoginUtils.getLocale()));
					userOpLogVo.setOpUsername(userInfo.getUserName());
					if(!StringUtils.isEmpty(userOpLogVo.getParams())){
					Map<String, Object> map = new LinkedHashMap<String, Object>();
					Map<String, Object> params= (Map<String, Object>) JSON.parseObject(userOpLogVo.getParams(), map.getClass());
					String opContent="";
					if(params!=null){
						for(Map.Entry<String,Object> param:params.entrySet()){
						   opContent+=BeanManager.getMessage(userOpLogVo.getModuleCode()+"."+param.getKey(), LoginUtils.getLocale());
						   opContent+=" "+param.getValue()+" ";
						}
					}
					userOpLogVo.setOpContent(opContent);
					}
				}
				return voList;
			}

		});
		return pageList;
	}

	@Override
	public void save(UserOpLogVo vo) {
		UserOpLog userOpLog = BeanCopier.copy(vo, UserOpLog.class);
		userOpLogDao.save(userOpLog);

	}

}
